package com.example.wac.controller;

import com.example.wac.annotation.UnJwtToken;
import com.example.wac.entity.common.ReqDTO;
import com.example.wac.service.WeChatService;
import com.example.wac.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author wuancheng
 * @description
 * @date 2022-07-25 14:18:00
 */

@Slf4j
@RestController
@RequestMapping("/we_chat")
public class WeChatController {

    @Autowired
    WeChatService weChatService;

    /**
     * get请求表示第一次接入微信服务器的验证
     *
     * @param request
     * @param response
     */
    @GetMapping("/call_back")
    @UnJwtToken
    public void getCallBack(HttpServletRequest request, HttpServletResponse response){
        // 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        try (PrintWriter out = response.getWriter()) {
            // 签名检测
            if (weChatService.checkSignature(signature, timestamp, nonce)){
                out.print(echostr);
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    /**
     * post请求表示微信服务器转发消息过来
     *
     * @param request
     * @param response
     */
    @PostMapping("/call_back")
    @UnJwtToken
    public void postCallBack(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter writer = response.getWriter()){
            String respXml = weChatService.processRequest(request);
            writer.println(respXml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据微信code获取用户信息
     *
     * @param wxCode
     * @return
     */
    @GetMapping("/get_user_info")
    @UnJwtToken
    public ApiResult getWeChatUserInfo(@RequestParam("wxCode") String wxCode){
        Map<String, Object> userInfoByWxCode = weChatService.getWeChatUserInfoByWxCode(wxCode);
        return ApiResult.success(userInfoByWxCode);
    }

    /**
     * 发送模板消息
     *
     * @param dto
     * @return
     */
    @PostMapping("/send_message")
    @UnJwtToken
    public Map<String, Object> sendTemplateMessage(@RequestBody ReqDTO dto){
      return weChatService.sendTemplateMessage(dto);
    }

    @GetMapping("/get_token")
    @UnJwtToken
    public String getToken(){
        String accessToken = weChatService.getAccessToken();
        return accessToken;
    }
}
